package com.example.wangyuyuan.androidframe.mvp.ui.presenter;

import com.example.wangyuyuan.androidframe.listener.OnLoadDataListListener;
import com.example.wangyuyuan.androidframe.mvp.entitry.LoginUserinfo;
import com.example.wangyuyuan.androidframe.mvp.ui.model.LoginModel;
import com.example.wangyuyuan.androidframe.mvp.ui.view.LoginView;

/**
 * Created by wangyuyuan on 2017/5/18.
 */

public class LoginPresenter implements OnLoadDataListListener<LoginUserinfo> {

    //创建单例
    private static class SingletonHolder {
        private static final LoginPresenter INSTANCE = new LoginPresenter();
    }

    //获取单例
    public static LoginPresenter getInstance() {
        return SingletonHolder.INSTANCE;
    }

    LoginView view;
    LoginModel model;

    public LoginPresenter() {
        model = new LoginModel();
    }

    public void login(String region, String phoneNumber, String pwd, LoginView mview) {
        view = mview;
        model.login(this, region, phoneNumber, pwd);
    }

    @Override
    public void onSuccess(LoginUserinfo data) {
        if (data.getCode() == 200) {
            view.loginSuccess(data);
        } else {
            view.loginFail("登陆失败");
        }
    }

    @Override
    public void onFailure(Throwable e) {
        view.loginFail(e.toString());
    }
}
